package com.vdong.trade.order.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.vdong.trade.order.common.contract.ApiContract;
import com.vdong.trade.order.common.exception.ProcessException;
import com.vdong.trade.order.common.foundation.FoundationController;
import com.vdong.trade.order.entity.dto.param.ChannelCurveParamDTO;
import com.vdong.trade.order.entity.dto.param.DistributionOrdersParamDTO;
import com.vdong.trade.order.entity.dto.result.DistributionOrdersResultDTO;
import com.vdong.trade.order.entity.dto.result.SellHotResultDTO;
import com.vdong.trade.order.entity.po.ChannelCurvePO;
import com.vdong.trade.order.entity.po.CountOrderPO;
import com.vdong.trade.order.entity.vo.*;
import com.vdong.trade.order.service.OrderService;
import com.vdong.trade.trading.facade.enums.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.math.ec.ScaleYPointMap;
import org.lamb.framework.common.userDTO.AccountSystemUserInfoDTO;
import org.lamb.framework.common.util.BeanPlasticityUtill;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.DateUtil;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.core.security.LambPrincipalFactoryContainer;
import org.lamb.framework.core.templete.LambResponseTemplete;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

import java.util.List;

import static com.vdong.trade.order.common.enums.ProcessExceptionEnum.EB00000005;
import static com.vdong.trade.order.common.enums.ProcessExceptionEnum.EB00000008;
import static com.vdong.trade.order.common.enums.ProcessExceptionEnum.EB00000012;

/**
 * @author zhoulongbiao
 * @Date 2018/12/13 17:44
 */
@RestController
@CrossOrigin
@Slf4j
public class OrderController extends FoundationController {

    @Resource
    private OrderService orderService;

    @RequestMapping(value = ApiContract.I000000001, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> channelCurve(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        log.info("channelCurve,入参为:[{}]", JSON.toJSONString(param));
        if (!StringUtil.hasBlank(param.getStartDate(), param.getEndDate())) {
            return returning(CopyUtil.copyList(orderService.channelCurve(ChannelCurveParamDTO.builder().
                    userId(param.getUserId()).startDate(DateUtil.stampToDate(param.getStartDate()))
                    .endDate(DateUtil.stampToDate(param.getEndDate())).build()), ChannelCurveVO.class));
        }
        return returning(CopyUtil.copyList(orderService.channelCurve(ChannelCurveParamDTO.builder().
                userId(param.getUserId()).build()), ChannelCurveVO.class));
    }

    /**
     * 分销订单列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = ApiContract.I000000002, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> DistributionOrderList(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        if (!StringUtil.hasBlank(param.getStoreId())) {
            AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
            param.setStoreId(accountSystemUser.getSysUserId());
        }
        return returning(CopyUtil.copyObject(orderService.DistributionOrderList(BeanPlasticityUtill.
                copy(DistributionOrdersParamDTO.class, param)), new PageInfo<DistributionOrderDetailVO>()));
    }

    @RequestMapping(value = ApiContract.I000000003, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> DistributionOrderDetail(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(orderService.DistributionOrderDetail
                (CopyUtil.copyObject(param, new DistributionOrdersParamDTO())), new DistributionOrderDetailVO()));
    }

    /**
     * 财务中心-收入明细列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = ApiContract.I000000004, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> DetailEarningList(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (accountSystemUser == null) {
            log.error("获取商户用户缓存信息失败!");
            return Mono.error(new ProcessException(EB00000012));
        }
        param.setStoreId(accountSystemUser.getSysUserId());
        return returning(CopyUtil.copyObject(orderService.DetailEarningList
                (CopyUtil.copyObject(param, new DistributionOrdersParamDTO())), new PageInfo<DetailEarningListVO>()));
    }

    @RequestMapping(value = ApiContract.I000000005, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> DetailEarningDetail(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));

        return returning(CopyUtil.copyObject(orderService.DetailEarningDetail(CopyUtil.copyObject(param,
                new DistributionOrdersParamDTO())), new DetailEarningDetailVO()));
    }

    /**
     * 渠道数据
     */
    @RequestMapping(value = ApiContract.I000000006, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> countOrder(@RequestBody CountOrderPO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(orderService.countOrder(param), new CountOrderVO()));
    }

    @RequestMapping(value = ApiContract.I000000007, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> peddleDistributionOrderList(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param)) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(orderService.peddleDistributionOrderList(BeanPlasticityUtill.
                copy(DistributionOrdersParamDTO.class, param)), new PageInfo<PeddleDistributionListVO>()));
    }

    /**
     * 财务中心 交易明细列表
     *
     * @param param
     * @return
     */
    @RequestMapping(value = ApiContract.I000000008, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> transactionList(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param.getStoreId(), param.getPageNum(), param.getPageSize()))
            return Mono.error(new ProcessException(EB00000008));
        AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
        if (accountSystemUser == null) {
            log.error("获取商户用户缓存信息失败!");
            return Mono.error(new ProcessException(EB00000012));
        }
        param.setStoreId(accountSystemUser.getSysUserId());
        return returning(CopyUtil.copyObject(orderService.transactionList(CopyUtil.copyObject(param,
                new DistributionOrdersParamDTO())), new PageInfo<DetailEarningListVO>()));

    }

    @RequestMapping(value = ApiContract.I000000009, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> transactionDetail(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param.getOrderId())) return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(orderService.transactionDetail(
                CopyUtil.copyObject(param, new DistributionOrdersParamDTO())), new TransactionDetailVO()));
    }

    /**
     * H5热销榜
     *
     * @return
     */
    @RequestMapping(value = ApiContract.I000000010, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> sellHot(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param.getType(), param.getPageNum(), param.getPageSize()))
            return Mono.error(new ProcessException(EB00000008));
        return returning(CopyUtil.copyObject(orderService.sellHot(BeanPlasticityUtill.
                copy(DistributionOrdersParamDTO.class, param)), new PageInfo<SellHotVO>()));
    }

    /**
     * 收入组成
     *
     * @return
     */
    @RequestMapping(value = ApiContract.I000000011, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> pusherEarnings(@RequestBody ChannelCurvePO param) {
        if (StringUtil.hasBlank(param.getUserId(), param.getPageNum(), param.getPageSize()))
            return Mono.error(new ProcessException(EB00000008));
        try {
            return returning(CopyUtil.copyObject(orderService.pusherEarnings(BeanPlasticityUtill.
                    copy(DistributionOrdersParamDTO.class, param)), new PageInfo<SillySoftAllVO>()));
        } catch (Exception e) {
            return returning(e.getMessage());
        }
    }
}
